package com.wqtz.chechai.view.activities;

import android.app.Activity;
import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.wqtz.chechai.R;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.presenter.LoginPresenter;
import com.wqtz.chechai.utils.StringUtils;
import com.wqtz.chechai.utils.ToastUtils;
import com.wqtz.chechai.view.impl.IloginViewImpl;
import com.wqtz.chechai.widget.ClearEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.hugeterry.updatefun.UpdateFunGO;
import cn.hugeterry.updatefun.config.UpdateKey;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/15 9:39
 */

public class LoginActivity extends BaseActivity {
	@BindView(R.id.tv_login_title)
	TextView mTvLoginTitle;
	@BindView(R.id.et_name)
	ClearEditText mEtName;
	@BindView(R.id.et_pwd)
	EditText mEtPwd;
	@BindView(R.id.btn_login)
	Button mBtnLogin;
	@BindView(R.id.tv_usemsm)
	TextView mTvUsemsm;
	LoginPresenter mLoginPresenter;
	String name;
	String pwd;
	@BindView(R.id.iv_eye)
	CheckBox mIvEye;


	@Override
	public void init() {
		ButterKnife.bind(this);//绑定Knife
		
	}

	@Override
	public void loadData() {
		mIvEye.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					//设置EditText文本为可见的
					mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				} else {
					//设置EditText文本为隐藏的
					mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
				}

			}
		});
		mLoginPresenter = new LoginPresenter(new IloginViewImpl() {
			SVProgressHUD mProgressHUD = new SVProgressHUD(LoginActivity.this);

			@Override
			public void moveToIndex() {
				Intent intent = new Intent(LoginActivity.this, MainActivity.class);
				startActivity(intent);
				LoginActivity.this.finish();
			}

			@Override
			public void cancel() {
				LoginActivity.this.finish();
			}

			@Override
			public void showLoading() {
				mProgressHUD.showWithStatus("加载中");
			}

			@Override
			public void hideLoading() {
				mProgressHUD.dismiss();
			}

			@Override
			public Activity getActivity() {
				return null;
			}

			@Override
			public void showError(String message, int code) {
				mProgressHUD.showErrorWithStatus(message);
				//ToastUtils.show(LoginActivity.this, message, 1000);
			}
		});

	}

	@Override
	public int setContentViews() {
		return R.layout.activity_login;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}

	public String getName() {
		if (StringUtils.isEmpty(mEtName.getText().toString())) {
			return null;
		}
		return mEtName.getText().toString();
	}


	public String getPwd() {
		if (StringUtils.isEmpty(mEtPwd.getText().toString())) {
			return null;
		}
		return mEtPwd.getText().toString();
	}


	@OnClick({R.id.btn_login, R.id.tv_usemsm})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.btn_login:
				name = getName();
				pwd = getPwd();
				if (StringUtils.isEmpty(name) || StringUtils.isEmpty(pwd)) {
					ToastUtils.show(LoginActivity.this, "账号或密码为空", 2000);
					break;
				}
				if (!StringUtils.isValidCellphone(name)) {
					ToastUtils.show(LoginActivity.this, "手机号格式不正确", 2000);
					break;
				}
				if (StringUtils.isValidPassword(pwd) && StringUtils.isValidPassword(pwd)&&StringUtils.ifValidSafePassword(pwd)) {
					mLoginPresenter.login(this, getName(), getPwd());
					break;
				} else {
					ToastUtils.show(LoginActivity.this, "密码只能为大于6位的英文字符", 2000);
				}

				break;
			case R.id.tv_usemsm:
				/**
				 * 短信验证码登录
				 */
				startActivity(new Intent(this,SmsVerificationLoginActivity.class));
				break;

		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		activitys.add(this); //添加当前的activity
	}
}
